import {areaAll} from "@/api/common";

export function area() {
  return new Promise(function (resolve, reject) {
    areaAll().then(response => {
      let res = recursionArea(response.data, 1);
      resolve(res);
    }).catch(function (error) {
      reject(error);
    });
  });
}


function recursionArea(data, pid) {
  let tree = [];
  let temp;
  for (let i = 0; i < data.length; i++) {
    if (data[i].parentId === pid) {
      let obj = {label: data[i].name, value: data[i].id};
      temp = recursionArea(data, data[i].id);
      if (temp.length > 0) {
        obj.children = temp;
      }
      tree.push(obj);
    }
  }
  return tree;
}
